import { useState, useCallback } from 'react'
import { 
  tree as treeApi,
  read as readApi,
  create as createApi,
  update as updateApi,
  del as delApi,
 } from '@/services/admin/role';
import { NORMAL_CODE } from '@/constants';

export default () => {
  const [minTotal, setMinTotal] = useState(0);
  const [minList, setMinList] = useState([]);
  const [total, setTotal] = useState(0);
  const [list, setList] = useState([]);
  const [data, setData] = useState({});

  // 简单列表
  const minLists = useCallback(async (param,attach={}) => {
    const res = await treeApi(param,attach.options??{});
    if (res.code === NORMAL_CODE) {
      setMinTotal(res.data.total);
      setMinList(res.data.list);
    }else{
      setMinTotal(0);
      setMinList([]);
    }
    return res;
  }, [])

  // 列表
  const lists = useCallback(async (param,attach={}) => {
    const res = await treeApi(param,attach.options??{});
    if (res.code === NORMAL_CODE) {
      setTotal(res.data.total);
      setList(res.data.list);
    }else{
      setTotal(0);
      setList([]);
    }
    return res;
  }, [])

  // 信息
  const read = useCallback(async (param,attach={}) => {
    const res = await readApi(param,attach.options??{});
    if (res.code === NORMAL_CODE) {
      setData(res.data);
    }else{
      setData({});
    }
    return res;
  }, [])

  // 新增
  const create = useCallback(async (param,attach={}) => {
    const res = await createApi(param,attach.options??{});
    return res;
  }, [])

  // 更新
  const update = useCallback(async (param,attach={}) => {
    const res = await updateApi(param,attach.options??{});
    return res;
  }, [])

  // 删除
  const del = useCallback(async (param,attach={}) => {
    const res = await delApi(param,attach.options??{});
    return res;
  }, [])

  return {
    data,
    setData,
    minTotal,
    minList,
    minLists,
    total,
    list,
    lists,
    read,
    create,
    update,
    del,
  }
}